<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">
<meta name="referrer" content="same-origin">
<meta name="referrer" content="no-referrer" />








<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Docker," />










<meta name="description" content="&amp;emsp;&amp;emsp;在学习了使用DockerFile制作一个简单的镜像并运行，我觉得接下去有必要了解一下Docker背后的运行原理，本着知其然而知其所以然，这样对后续学习或者使用Docker部署应用都有一定裨益。我们已经知道Docker是一个使用了虚拟化技术的容器，同虚拟机一样有着资源隔离和分配的优点，但是因为本身架构的不同，比虚拟机更加高效和便捷。既然如此，那么Docker是如何实现的？一切">
<meta name="keywords" content="Docker">
<meta property="og:type" content="article">
<meta property="og:title" content="拥抱Docker（三）基础原理之Namespaces">
<meta property="og:url" content="http://yoursite.com/2019/07/08/拥抱Docker（三）基础原理之Namespaces/index.html">
<meta property="og:site_name" content="矩阵编程">
<meta property="og:description" content="&amp;emsp;&amp;emsp;在学习了使用DockerFile制作一个简单的镜像并运行，我觉得接下去有必要了解一下Docker背后的运行原理，本着知其然而知其所以然，这样对后续学习或者使用Docker部署应用都有一定裨益。我们已经知道Docker是一个使用了虚拟化技术的容器，同虚拟机一样有着资源隔离和分配的优点，但是因为本身架构的不同，比虚拟机更加高效和便捷。既然如此，那么Docker是如何实现的？一切">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-10-24T16:22:02.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="拥抱Docker（三）基础原理之Namespaces">
<meta name="twitter:description" content="&amp;emsp;&amp;emsp;在学习了使用DockerFile制作一个简单的镜像并运行，我觉得接下去有必要了解一下Docker背后的运行原理，本着知其然而知其所以然，这样对后续学习或者使用Docker部署应用都有一定裨益。我们已经知道Docker是一个使用了虚拟化技术的容器，同虚拟机一样有着资源隔离和分配的优点，但是因为本身架构的不同，比虚拟机更加高效和便捷。既然如此，那么Docker是如何实现的？一切">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/07/08/拥抱Docker（三）基础原理之Namespaces/"/>





  <title>拥抱Docker（三）基础原理之Namespaces | 矩阵编程</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?7d29e6fba8e07d1a8af2414a20e5f2a6";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">矩阵编程</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-主页">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-主页"></i> <br />
            
            主页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-分类"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-所有文章">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-文章"></i> <br />
            
            所有文章
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/07/08/拥抱Docker（三）基础原理之Namespaces/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="matrix">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="矩阵编程">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">拥抱Docker（三）基础原理之Namespaces</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-07-08T13:39:05+08:00">
                2019-07-08
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Docker/" itemprop="url" rel="index">
                    <span itemprop="name">Docker</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/07/08/拥抱Docker（三）基础原理之Namespaces/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2019/07/08/拥抱Docker（三）基础原理之Namespaces/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&emsp;&emsp;在学习了使用DockerFile制作一个简单的镜像并运行，我觉得接下去有必要了解一下Docker背后的运行原理，本着知其然而知其所以然，这样对后续学习或者使用Docker部署应用都有一定裨益。我们已经知道Docker是一个使用了虚拟化技术的容器，同虚拟机一样有着资源隔离和分配的优点，但是因为本身架构的不同，比虚拟机更加高效和便捷。既然如此，那么Docker是如何实现的？一切都还要从Linux的虚拟化技术说起。<br><a id="more"></a></p>
<h4 id="Namespaces（命名空间）"><a href="#Namespaces（命名空间）" class="headerlink" title="Namespaces（命名空间）"></a>Namespaces（命名空间）</h4><p>&emsp;&emsp;Namespaces是Linux Kernel的一个功能，它可以隔离一系列的系统资源，比如PID（进程）、Network（网络）、User ID(用户)、Mount（挂载）等等。Linux一共有六种类型的Namespaces：</p>
<table>
<thead>
<tr>
<th style="text-align:center">类型</th>
<th style="text-align:center">参数</th>
<th style="text-align:center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">PID</td>
<td style="text-align:center">CLONE_NEWPID</td>
<td style="text-align:center">进程ID（Linux 2.6.24）</td>
</tr>
<tr>
<td style="text-align:center">Mount</td>
<td style="text-align:center">CLONE_NEWNS</td>
<td style="text-align:center">磁盘挂载（Linux 2.4.19）</td>
</tr>
<tr>
<td style="text-align:center">Network</td>
<td style="text-align:center">CLONE_NEWNET</td>
<td style="text-align:center">网络（Linux 2.4.19）</td>
</tr>
<tr>
<td style="text-align:center">IPC</td>
<td style="text-align:center">CLONE_NEWIPC</td>
<td style="text-align:center">进程间通信，System V IPC和POSIX message queues（Linux 2.6.19）</td>
</tr>
<tr>
<td style="text-align:center">UTS</td>
<td style="text-align:center">CLONE_NEWUTS</td>
<td style="text-align:center">hostname和domainname（Linux 2.6.19）</td>
</tr>
<tr>
<td style="text-align:center">User</td>
<td style="text-align:center">CLONE_NEWUSER</td>
<td style="text-align:center">用户组ID（Linux 3.8）</td>
</tr>
</tbody>
</table>
<p>&emsp;&emsp;上面这六种Namespaces主要由以下三种函数调用方式：</p>
<ol>
<li>clone() 创建新进程。</li>
<li>unshare() 将进程移出某个Namespaces。</li>
<li>setns() 将进程加入到某个Namespaces。</li>
</ol>
<h5 id="PID-Namespace"><a href="#PID-Namespace" class="headerlink" title="PID Namespace"></a>PID Namespace</h5><p>&emsp;&emsp;PID Namespace用于隔离进程ID，同一个进程在不同的PID Namespace中有不同的进程ID，比如在Dokcer的容器中，通常可以看到运行中的那个进程的PID为1，PID为1的进程在Linux中认为是init进程，是第一个用户进程。以<a href="/2019/06/04/拥抱Docker（二）Dockerfile制作镜像">拥抱Docker（二）Dockerfile制作镜像</a>中制作的nginx镜像为例，进入容器查看nginx进程PID：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">luyundeMacBook-Pro:~ luyun$ docker ps</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES</span><br><span class="line">28c635fa7ed8        nginx:baidu         <span class="string">"nginx -g 'daemon of…"</span>   5 minutes ago       Up 5 minutes        0.0.0.0:80-&gt;80/tcp   romantic_bassi</span><br><span class="line">luyundeMacBook-Pro:~ luyun$ docker <span class="built_in">exec</span> -it 28c635fa7ed8 /bin/sh</span><br><span class="line">/ <span class="comment"># ps -ef</span></span><br><span class="line">PID   USER     TIME  COMMAND</span><br><span class="line">    1 root      0:00 nginx: master process nginx -g daemon off;</span><br><span class="line">    6 nginx     0:00 nginx: worker process</span><br><span class="line">    7 nginx     0:00 nginx: worker process</span><br><span class="line">   20 root      0:00 /bin/sh</span><br><span class="line">   26 root      0:00 ps -ef</span><br></pre></td></tr></table></figure></p>
<p>可以看到nginx的master process的PID为1，但是这与我们在容器外面看到的nginx PID不同：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">luyundeMacBook-Pro:~ luyun$ docker top 53d612113e3a</span><br><span class="line">PID                 USER                TIME                COMMAND</span><br><span class="line">2576                root                0:00                nginx: master process nginx -g daemon off;</span><br><span class="line">2616                rpc                 0:00                nginx: worker process</span><br><span class="line">2617                rpc                 0:00                nginx: worker process</span><br></pre></td></tr></table></figure></p>
<p>这证明容器中的PID Namespace和外面是不同的，那么Docker是如何更改这个PID Namespace？我尝试用Go写了一个小例子：<br><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ns.go</span></span><br><span class="line"><span class="keyword">package</span> main</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"os"</span></span><br><span class="line">    <span class="string">"os/exec"</span></span><br><span class="line">    <span class="string">"syscall"</span></span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    cmd := exec.Command(<span class="string">"sh"</span>)</span><br><span class="line">    cmd.SysProcAttr = &amp;syscall.SysProcAttr&#123;</span><br><span class="line">        Cloneflags: syscall.CLONE_NEWPID,</span><br><span class="line">    &#125;</span><br><span class="line">    cmd.Stdin = os.Stdin</span><br><span class="line">    cmd.Stdout = os.Stdout</span><br><span class="line">    cmd.Stderr = os.Stderr</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> err := cmd.Run(); err != <span class="literal">nil</span>&#123;</span><br><span class="line">        fmt.Print(err)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>注意这个例子必须要在Linux环境运行（以下以及后面的程序都是在Centos上运行）：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos go-workspace]<span class="comment"># go run ns.go</span></span><br><span class="line">sh-4.2<span class="comment"># echo $$</span></span><br><span class="line">1</span><br></pre></td></tr></table></figure></p>
<p>使用echo打印了main函数运行的进程，它的PID为1，注意这里不能用之前的ps命令去查看，因为ps命令会使用/proc中的内容，而/proc在这个程序中还没有进行隔离，等会在Mount Namespace会说到。打开另外一个命令窗口（实际因为未隔离/proc在原窗口查看也行），运行ps命令，可以看到：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos go-workspace]<span class="comment"># ps -ef|grep ns</span></span><br><span class="line">root     27265  1921  0 19:17 pts/0    00:00:00 go run ns.go</span><br><span class="line">root     27284 27265  0 19:17 pts/0    00:00:00 /tmp/go-build269986868/b001/exe/ns</span><br></pre></td></tr></table></figure></p>
<p>这个main函数在宿主机上运行的PID是27284。</p>
<h5 id="Mount-Namespace"><a href="#Mount-Namespace" class="headerlink" title="Mount Namespace"></a>Mount Namespace</h5><p>&emsp;&emsp;先说一下什么叫Mount（挂载），在Linux中只有一个根目录，凡是根目录以外的文件想要被系统访问，就必须关联到这个根目录下的某个目录，这种关联动作就称之为“挂载”，而这个目录称之为“挂载点”。挂载的实质是为访问添加入口，挂载点目录在挂载之后有几个特性需要注意：</p>
<ol>
<li>该目录下原有的文件会被隐藏。</li>
<li>该目录不可被其他进程使用。</li>
</ol>
<p>&emsp;&emsp;Mount Namespace可以使不同的进程看到不一样的文件挂载视图，下面我们就使用宿主机的/proc进行挂载。修改一下ns.go文件，新增<strong>CLONE_NEWNS</strong>：<br><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ns.go</span></span><br><span class="line"><span class="keyword">package</span> main</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"os"</span></span><br><span class="line">    <span class="string">"os/exec"</span></span><br><span class="line">    <span class="string">"syscall"</span></span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    cmd := exec.Command(<span class="string">"sh"</span>)</span><br><span class="line">    cmd.SysProcAttr = &amp;syscall.SysProcAttr&#123;</span><br><span class="line">        Cloneflags: syscall.CLONE_NEWPID | syscall.CLONE_NEWNS,</span><br><span class="line">    &#125;</span><br><span class="line">    cmd.Stdin = os.Stdin</span><br><span class="line">    cmd.Stdout = os.Stdout</span><br><span class="line">    cmd.Stderr = os.Stderr</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> err := cmd.Run(); err != <span class="literal">nil</span>&#123;</span><br><span class="line">        fmt.Print(err)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行ns.go：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos go-workspace]<span class="comment"># go run ns.go </span></span><br><span class="line">sh-4.2<span class="comment"># ls /proc</span></span><br><span class="line">1    17     2149  226  27901  337  41	548   768   8039  buddyinfo  diskstats	  iomem      kpagecount  mounts        self	      timer_list</span><br><span class="line">10   18     216   235  27902  361  484	558   776   8048  bus	     dma	  ioports    kpageflags  mtrr	       slabinfo       timer_stats</span><br><span class="line">117  18823  2191  25   28     367  5	559   7788  8066  cgroups    driver	  irq	     loadavg	 net	       softirqs       tty</span><br><span class="line">12   18834  2206  256  2862   37   503	576   785   8069  cmdline    execdomains  kallsyms   locks	 pagetypeinfo  <span class="built_in">stat</span>	      uptime</span><br><span class="line">13   19     2207  26   29     38   505	60    8     8074  consoles   fb		  kcore      mdstat	 partitions    swaps	      version</span><br><span class="line">14   2	    222   261  3      383  508	6695  802   8081  cpuinfo    filesystems  keys	     meminfo	 sched_debug   sys	      vmallocinfo</span><br><span class="line">15   2133   223   262  3037   39   510	7     803   9	  crypto     fs		  key-users  misc	 schedstat     sysrq-trigger  vmstat</span><br><span class="line">16   214    224   27   3038   40   513	7238  8038  acpi  devices    interrupts   kmsg	     modules	 scsi	       sysvipc	      zoneinfo</span><br></pre></td></tr></table></figure></p>
<p>可以看到/proc下有一堆文件，然后我们在sh中使用mount指令将proc系统挂载到/proc中：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">sh-4.2<span class="comment"># mount -t proc proc /proc</span></span><br><span class="line">sh-4.2<span class="comment"># ls /proc</span></span><br><span class="line">1	   cgroups   devices	  fb	       ioports	 key-users   locks    mounts	    sched_debug  softirqs	sysvipc      version</span><br><span class="line">4	   cmdline   diskstats	  filesystems  irq	 kmsg	     mdstat   mtrr	    schedstat	 <span class="built_in">stat</span>		timer_list   vmallocinfo</span><br><span class="line">acpi	   consoles  dma	  fs	       kallsyms  kpagecount  meminfo  net	    scsi	 swaps		timer_stats  vmstat</span><br><span class="line">buddyinfo  cpuinfo   driver	  interrupts   kcore	 kpageflags  misc     pagetypeinfo  self	 sys		tty	     zoneinfo</span><br><span class="line">bus	   crypto    execdomains  iomem        keys	 loadavg     modules  partitions    slabinfo	 sysrq-trigger	uptime</span><br></pre></td></tr></table></figure></p>
<p>可以看到现在/proc少了很多文件，或者可以使用之前的ps命令查看，会发现系统少了很多进程：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">sh-4.2<span class="comment"># ps -ef</span></span><br><span class="line">UID        PID  PPID  C STIME TTY          TIME CMD</span><br><span class="line">root         1     0  0 19:49 pts/0    00:00:00 sh</span><br><span class="line">root         6     1  0 19:55 pts/0    00:00:00 ps -ef</span><br></pre></td></tr></table></figure></p>
<p>当我们使用exit退出这个sh的时候，再查看/proc的时候会报错：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">sh-4.2<span class="comment"># exit</span></span><br><span class="line"><span class="built_in">exit</span></span><br><span class="line">[root@VM_0_12_centos go-workspace]<span class="comment"># ls /proc</span></span><br><span class="line">ls: 无法读取符号链接/proc/self: 没有那个文件或目录</span><br></pre></td></tr></table></figure></p>
<p>或者使用ps命令查看系统进程：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos go-workspace]<span class="comment"># ps -ef</span></span><br><span class="line">Error, <span class="keyword">do</span> this: mount -t proc proc /proc</span><br></pre></td></tr></table></figure></p>
<p>按照提示再次输入mount指令，一切又恢复正常了：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos go-workspace]<span class="comment"># mount -t proc proc /proc</span></span><br><span class="line">[root@VM_0_12_centos go-workspace]<span class="comment"># ls /proc</span></span><br><span class="line">1    17     2149  226  27901  337  41   548  7788  8679  buddyinfo  diskstats    iomem      kpagecount  mounts        self           timer_list</span><br><span class="line">10   18     216   235  27902  361  484  558  785   9     bus        dma          ioports    kpageflags  mtrr          slabinfo       timer_stats</span><br><span class="line">117  18823  2191  25   28     367  5    559  8     9349  cgroups    driver       irq        loadavg     net           softirqs       tty</span><br><span class="line">12   18834  2206  256  2862   37   503  576  802   9580  cmdline    execdomains  kallsyms   locks       pagetypeinfo  <span class="built_in">stat</span>           uptime</span><br><span class="line">13   19     2207  26   29     38   505  60   803   9582  consoles   fb           kcore      mdstat      partitions    swaps          version</span><br><span class="line">14   2      222   261  3      383  508  7    8394  9583  cpuinfo    filesystems  keys       meminfo     sched_debug   sys            vmallocinfo</span><br><span class="line">15   2133   223   262  3037   39   510  768  8676  9976  crypto     fs           key-users  misc        schedstat     sysrq-trigger  vmstat</span><br><span class="line">16   214    224   27   3038   40   513  776  8678  acpi  devices    interrupts   kmsg       modules     scsi          sysvipc        zoneinfo</span><br></pre></td></tr></table></figure></p>
<h5 id="IPC-Namespace"><a href="#IPC-Namespace" class="headerlink" title="IPC Namespace"></a>IPC Namespace</h5><p>&emsp;&emsp;IPC Namespace用于隔离System V IPC对象和POSIX message queues，System V IPC对象包含共享内存、信号量和消息队列，关于System V IPC和POSIX message queues的区别可以参照<a href="https://stackoverflow.com/questions/4582968/system-v-ipc-vs-posix-ipc" target="_blank" rel="noopener">《system-v-ipc-vs-posix-ipc》</a>。微调一下之前的代码，加入syscall.CLONE_NEWIPC：<br><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"os"</span></span><br><span class="line">    <span class="string">"os/exec"</span></span><br><span class="line">    <span class="string">"syscall"</span></span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    cmd := exec.Command(<span class="string">"sh"</span>)</span><br><span class="line">    cmd.SysProcAttr = &amp;syscall.SysProcAttr&#123;</span><br><span class="line">        Cloneflags: syscall.CLONE_NEWPID | syscall.CLONE_NEWNS | syscall.CLONE_NEWIPC,</span><br><span class="line">    &#125;</span><br><span class="line">    cmd.Stdin = os.Stdin</span><br><span class="line">    cmd.Stdout = os.Stdout</span><br><span class="line">    cmd.Stderr = os.Stderr</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> err := cmd.Run(); err != <span class="literal">nil</span>&#123;</span><br><span class="line">        fmt.Print(err)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>按照下面的方式运行ns.go，可以看到：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos go-workspace]<span class="comment"># go run ns.go </span></span><br><span class="line">sh-4.2<span class="comment"># ipcs -s</span></span><br><span class="line"></span><br><span class="line">--------- 信号量数组 -----------</span><br><span class="line">键        semid      拥有者  权限     nsems</span><br></pre></td></tr></table></figure></p>
<p>现在我们创建一个message queue，并分别在宿主机和ns.go运行的shell环境中查看：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 先在ns.go中创建一个message queue</span></span><br><span class="line">sh-4.2<span class="comment"># ipcmk -Q</span></span><br><span class="line">消息队列 id：0</span><br><span class="line">sh-4.2<span class="comment"># ipcs -q</span></span><br><span class="line"></span><br><span class="line">--------- 消息队列 -----------</span><br><span class="line">键        msqid      拥有者  权限     已用字节数 消息      </span><br><span class="line">0x03ff4597 0          root       644        0            0</span><br></pre></td></tr></table></figure></p>
<p>在宿主机的shell中却查看不到这个mq，由此可见消息被隔离了：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos ~]<span class="comment"># ipcs -q</span></span><br><span class="line"></span><br><span class="line">--------- 消息队列 -----------</span><br><span class="line">键        msqid      拥有者  权限     已用字节数 消息</span><br></pre></td></tr></table></figure></p>
<h5 id="UTS-Namespace"><a href="#UTS-Namespace" class="headerlink" title="UTS Namespace"></a>UTS Namespace</h5><p>&emsp;&emsp;UTS Namespace主要用于完成对hostname和NIS domain name的隔离，修改一下上面的ns.go文件：<br><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"os"</span></span><br><span class="line">    <span class="string">"os/exec"</span></span><br><span class="line">    <span class="string">"syscall"</span></span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    cmd := exec.Command(<span class="string">"sh"</span>)</span><br><span class="line">    cmd.SysProcAttr = &amp;syscall.SysProcAttr&#123;</span><br><span class="line">        Cloneflags: syscall.CLONE_NEWPID | syscall.CLONE_NEWNS | syscall.CLONE_NEWIPC | syscall.CLONE_NEWUTS,</span><br><span class="line">    &#125;</span><br><span class="line">    cmd.Stdin = os.Stdin</span><br><span class="line">    cmd.Stdout = os.Stdout</span><br><span class="line">    cmd.Stderr = os.Stderr</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> err := cmd.Run(); err != <span class="literal">nil</span>&#123;</span><br><span class="line">        fmt.Print(err)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行ns.go设置hostname：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos go-workspace]<span class="comment"># go run ns.go </span></span><br><span class="line">sh-4.2<span class="comment"># hostname</span></span><br><span class="line">VM_0_12_centos</span><br><span class="line">sh-4.2<span class="comment"># hostname -b matrix</span></span><br><span class="line">sh-4.2<span class="comment"># hostname</span></span><br><span class="line">matrix</span><br></pre></td></tr></table></figure></p>
<p>再查看宿主机的hostname：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos ~]<span class="comment"># hostname</span></span><br><span class="line">VM_0_12_centos</span><br></pre></td></tr></table></figure></p>
<p>可以看到宿主机上的hostname没有改变。</p>
<h5 id="User-Namespace"><a href="#User-Namespace" class="headerlink" title="User Namespace"></a>User Namespace</h5><p>&emsp;&emsp;User Namespace用于隔离用户组的ID，一个进程的用户组ID在不同的User Namespace是不同的，比如某个进程在一个User Namespace是拥有root权限，但是在另外一个User Namespace却没有这个超级权限。为了演示Docker如何修改User Namespace，稍微修改一下上面的代码，在SysProcAttr中增加一个CLONE_NEWUSER参数：<br><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"os"</span></span><br><span class="line">    <span class="string">"os/exec"</span></span><br><span class="line">    <span class="string">"syscall"</span></span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    cmd := exec.Command(<span class="string">"sh"</span>)</span><br><span class="line">    cmd.SysProcAttr = &amp;syscall.SysProcAttr&#123;</span><br><span class="line">        Cloneflags: syscall.CLONE_NEWPID | syscall.CLONE_NEWUSER,</span><br><span class="line">    &#125;</span><br><span class="line">    cmd.Stdin = os.Stdin</span><br><span class="line">    cmd.Stdout = os.Stdout</span><br><span class="line">    cmd.Stderr = os.Stderr</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> err := cmd.Run(); err != <span class="literal">nil</span>&#123;</span><br><span class="line">        fmt.Print(err)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行命令：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos go-workspace]<span class="comment"># go run ns.go </span></span><br><span class="line">fork/<span class="built_in">exec</span> /usr/bin/sh: invalid argument</span><br></pre></td></tr></table></figure></p>
<p>这里报invalid argument的错误，User namespace是Linux内核3.8版本之后支持的，由于并不成熟，在很多Linux机器上并未开启，比如我这台Centos。可以输入以下指令开启：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos ~]<span class="comment"># grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"</span></span><br></pre></td></tr></table></figure></p>
<p>然后重启系统，重新运行上面的指令：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos go-workspace]<span class="comment"># go run ns.go </span></span><br><span class="line">sh-4.2$ id</span><br><span class="line">uid=65534 gid=65534 组=65534</span><br></pre></td></tr></table></figure></p>
<p>输入“id”指令，可以看到用户组为65534，而在另外一个窗口以root身份运行“id”指令可以看到：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos go-workspace]<span class="comment"># id</span></span><br><span class="line">uid=0(root) gid=0(root) 组=0(root)</span><br></pre></td></tr></table></figure></p>
<h5 id="Network-Namespace"><a href="#Network-Namespace" class="headerlink" title="Network Namespace"></a>Network Namespace</h5><p>&emsp;&emsp;Network Namespace用于隔离网络设备相关，有了Network Namespace，每个容器就可以拥有自己独立的虚拟网络，比如有自己的IP、端口等。修改ns.go文件，加入syscall.CLONE_NEWNET：<br><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"os"</span></span><br><span class="line">    <span class="string">"os/exec"</span></span><br><span class="line">    <span class="string">"syscall"</span></span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    cmd := exec.Command(<span class="string">"sh"</span>)</span><br><span class="line">    cmd.SysProcAttr = &amp;syscall.SysProcAttr&#123;</span><br><span class="line">        Cloneflags: syscall.CLONE_NEWPID | syscall.CLONE_NEWNS | syscall.CLONE_NEWIPC | syscall.CLONE_NEWUTS | syscall.CLONE_NEWNET,</span><br><span class="line">    &#125;</span><br><span class="line">    cmd.Stdin = os.Stdin</span><br><span class="line">    cmd.Stdout = os.Stdout</span><br><span class="line">    cmd.Stderr = os.Stderr</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> err := cmd.Run(); err != <span class="literal">nil</span>&#123;</span><br><span class="line">        fmt.Print(err)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行ns.go，查看网络：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos go-workspace]<span class="comment"># go run ns.go </span></span><br><span class="line">sh-4.2<span class="comment"># ifconfig</span></span><br><span class="line">sh-4.2<span class="comment">#</span></span><br></pre></td></tr></table></figure></p>
<p>可以看到什么都没有输出，再看一下宿主机上的网络：<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">[root@VM_0_12_centos go-workspace]<span class="comment"># ifconfig</span></span><br><span class="line">eth0: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500</span><br><span class="line">        inet xx.xx.xx.xx  netmask 255.255.240.0  broadcast xx.xx.xx.xx</span><br><span class="line">        ether xx:xx:xx:xx:xx:xx  txqueuelen 1000  (Ethernet)</span><br><span class="line">        RX packets 3317648  bytes 273841657 (261.1 MiB)</span><br><span class="line">        RX errors 0  dropped 0  overruns 0  frame 0</span><br><span class="line">        TX packets 3349385  bytes 450207961 (429.3 MiB)</span><br><span class="line">        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0</span><br><span class="line"></span><br><span class="line">lo: flags=73&lt;UP,LOOPBACK,RUNNING&gt;  mtu 65536</span><br><span class="line">        inet 127.0.0.1  netmask 255.0.0.0</span><br><span class="line">        loop  txqueuelen 1  (Local Loopback)</span><br><span class="line">        RX packets 1018  bytes 159607 (155.8 KiB)</span><br><span class="line">        RX errors 0  dropped 0  overruns 0  frame 0</span><br><span class="line">        TX packets 1018  bytes 159607 (155.8 KiB)</span><br><span class="line">        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0</span><br></pre></td></tr></table></figure></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Docker/" rel="tag"># Docker</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/06/28/谈谈Spring的IoC之循环依赖/" rel="next" title="谈谈Spring的IoC之循环依赖">
                <i class="fa fa-chevron-left"></i> 谈谈Spring的IoC之循环依赖
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/07/11/Kubernetes学习笔记（一）一个例子展开/" rel="prev" title="Kubernetes学习笔记（一）一个例子展开">
                Kubernetes学习笔记（一）一个例子展开 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">matrix</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">47</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">18</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#Namespaces（命名空间）"><span class="nav-number">1.</span> <span class="nav-text">Namespaces（命名空间）</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#PID-Namespace"><span class="nav-number">1.1.</span> <span class="nav-text">PID Namespace</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Mount-Namespace"><span class="nav-number">1.2.</span> <span class="nav-text">Mount Namespace</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#IPC-Namespace"><span class="nav-number">1.3.</span> <span class="nav-text">IPC Namespace</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#UTS-Namespace"><span class="nav-number">1.4.</span> <span class="nav-text">UTS Namespace</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#User-Namespace"><span class="nav-number">1.5.</span> <span class="nav-text">User Namespace</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Network-Namespace"><span class="nav-number">1.6.</span> <span class="nav-text">Network Namespace</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">matrix</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>
<div class=BbeiAn-info">
  浙ICP备 -
  <a href="http://www.beian.miit.gov.cn">18040498号</a>
  </a>
</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  

    
      <script id="dsq-count-scr" src="https://luyun.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://yoursite.com/2019/07/08/拥抱Docker（三）基础原理之Namespaces/';
          this.page.identifier = '2019/07/08/拥抱Docker（三）基础原理之Namespaces/';
          this.page.title = '拥抱Docker（三）基础原理之Namespaces';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://luyun.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
